﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

using CCWin;
using BookModel;

namespace WinFromBook
{
    public partial class FrmTypeInfo : CCSkinMain
    {
        public FrmTypeInfo()
        {
            InitializeComponent();
        }
        #region 封装方法，执行操作
        public void GetTypeInfo()
        {
            this.skinListViewTypeInfo.Items.Clear();
            foreach (var item in new BLL.TypeInfoBLL().GetTYpeInfo())
            {
                ListViewItem lvi = new ListViewItem(item.TID + "");
                lvi.SubItems.Add(item.TName);
                lvi.SubItems.Add(item.Tremark);
                this.skinListViewTypeInfo.Items.Add(lvi);
            }
        }
        public void TextClear()
        {
            skinTextID.Text = "";
            skinTextName.Text = "";
            textRemark.Text = "";
        }
        public void InsertType()
        {
            if (String.IsNullOrEmpty(skinTextName.Text.Trim()))
            {
                MessageBox.Show("类型名称不能为空", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
                return;
            }
            TypeInfo ti = new TypeInfo()
            {
                TName = skinTextName.Text,
                Tremark = textRemark.Text.Trim()
            };
            string sql = new BLL.TypeInfoBLL().InsertTypeInfo(ti);
            MessageBox.Show(sql, "系统提示", MessageBoxButtons.OK);
            GetTypeInfo();
            if (sql.Contains("成功"))
            {
                TextClear();
            }
        }
        public void UpdateType()
        {
            if (String.IsNullOrEmpty(skinTextName.Text.Trim()))
            {
                MessageBox.Show("类型名称不能为空", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
                return;
            }
            TypeInfo ti = new TypeInfo()
            {
                TID = int.Parse(skinTextID.Text.Trim()),
                TName = skinTextName.Text,
                Tremark = textRemark.Text.Trim()
            };
            string sql = new BLL.TypeInfoBLL().UpdateTypeInfo(ti);
            MessageBox.Show(sql, "系统提示", MessageBoxButtons.OK);
            GetTypeInfo();
            if (sql.Contains("成功"))
            {
                TextClear();
            }
        }
        public void LVClick()
        {
            if (this.skinListViewTypeInfo.SelectedItems.Count <= 0)
                return;
            skinTextID.Text = skinListViewTypeInfo.SelectedItems[0].SubItems[0].Text;
            skinTextName.Text = skinListViewTypeInfo.SelectedItems[0].SubItems[1].Text;
            textRemark.Text = skinListViewTypeInfo.SelectedItems[0].SubItems[2].Text;
        }
        #endregion
        private void FrmType_Load(object sender, EventArgs e)
        {
            GetTypeInfo();
        }

        private void skinBuOK_Click(object sender, EventArgs e)
        {
            if (String.IsNullOrEmpty(skinTextID.Text.Trim()))
                InsertType();
            else
                UpdateType();
        }

        private void skinBuClear_Click(object sender, EventArgs e)
        {
            TextClear();
        }

        private void skinListViewTypeInfo_Click(object sender, EventArgs e)
        {
            LVClick();
        }
    }
}
